import axios from "./axios";

// 通用接口：获取未读通知数量
export const getUnreadCount = () => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = "/admin/notifications/unread-count";
  } else if (userRole === "hotel") {
    apiPath = "/hotel-admin/notifications/unread-count";
  } else if (userRole === "merchant") {
    apiPath = "/merchant/notifications/unread-count";
  } else {
    apiPath = "/user/notifications/unread-count";
  }

  return axios.get(apiPath);
};

// 通用接口：获取通知列表
export const getNotifications = (params) => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = "/admin/notifications";
  } else if (userRole === "hotel") {
    apiPath = "/hotel-admin/notifications";
  } else if (userRole === "merchant") {
    apiPath = "/merchant/notifications";
  } else {
    apiPath = "/user/notifications";
  }

  return axios.get(apiPath, { params });
};

// 通用接口：获取实时通知
export const getRealtimeNotifications = () => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = "/admin/notifications/realtime";
  } else if (userRole === "hotel") {
    apiPath = "/hotel-admin/notifications/realtime";
  } else if (userRole === "merchant") {
    apiPath = "/merchant/notifications/realtime";
  } else {
    apiPath = "/user/notifications/realtime";
  }

  return axios.get(apiPath);
};

// 通用接口：标记单个通知为已读
export const markAsRead = (id) => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = `/admin/notifications/${id}/read`;
  } else if (userRole === "hotel") {
    apiPath = `/hotel-admin/notifications/${id}/read`;
  } else if (userRole === "merchant") {
    apiPath = `/merchant/notifications/${id}/read`;
  } else {
    apiPath = `/user/notifications/${id}/read`;
  }

  return axios.put(apiPath);
};

// 通用接口：标记所有通知为已读
export const markAllAsRead = () => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = "/admin/notifications/read-all";
  } else if (userRole === "hotel") {
    apiPath = "/hotel-admin/notifications/read-all";
  } else if (userRole === "merchant") {
    apiPath = "/merchant/notifications/read-all";
  } else {
    apiPath = "/user/notifications/read-all";
  }

  return axios.put(apiPath);
};

// 通用接口：删除单个通知
export const deleteNotification = (id) => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = `/admin/notifications/${id}`;
  } else if (userRole === "hotel") {
    apiPath = `/hotel-admin/notifications/${id}`;
  } else if (userRole === "merchant") {
    apiPath = `/merchant/notifications/${id}`;
  } else {
    apiPath = `/user/notifications/${id}`;
  }

  return axios.delete(apiPath);
};

// 通用接口：清空所有通知
export const clearAllNotifications = () => {
  // 根据用户类型获取不同的API路径
  const userRole = localStorage.getItem("userRole") || "user";
  let apiPath = "";

  if (userRole === "admin") {
    apiPath = "/admin/notifications/clear-all";
  } else if (userRole === "hotel") {
    apiPath = "/hotel-admin/notifications/clear-all";
  } else if (userRole === "merchant") {
    apiPath = "/merchant/notifications/clear-all";
  } else {
    apiPath = "/user/notifications/clear-all";
  }

  return axios.delete(apiPath);
};

// 商户专用：获取通知设置
export const getMerchantNotificationSettings = () => {
  return axios.get("/merchant/notification/settings");
};

// 商户专用：保存通知设置
export const saveMerchantNotificationSettings = (settings) => {
  return axios.post("/merchant/notification/settings", settings);
};

// 酒店商户专用：获取通知设置
export const getHotelNotificationSettings = () => {
  return axios.get("/hotel-admin/notification/settings");
};

// 酒店商户专用：保存通知设置
export const saveHotelNotificationSettings = (settings) => {
  return axios.post("/hotel-admin/notification/settings", settings);
};

// 管理员专用：获取通知设置
export const getAdminNotificationSettings = () => {
  return axios.get("/admin/notification/settings");
};

// 管理员专用：保存通知设置
export const saveAdminNotificationSettings = (settings) => {
  return axios.post("/admin/notification/settings", settings);
};

// 管理员专用：发送系统通知
export const sendSystemNotification = (notification) => {
  return axios.post("/admin/notifications/send", notification);
};

// 用户专用：获取通知偏好设置
export const getUserNotificationPreferences = () => {
  return axios.get("/user/notification-preferences");
};

// 用户专用：保存通知偏好设置
export const saveUserNotificationPreferences = (preferences) => {
  return axios.post("/user/notification-preferences", preferences);
}; 